import axios from "axios";
import {ElMessage} from "element-plus"
import { getToken } from "./auth";
// console.log('MODE',import.meta.env.MODE);
const request = axios.create({
    
    baseURL: import.meta.env.VITE_API_BASEURL,
        // import.meta.env.MODE === "development"
        // ? "http://127.0.0.1:8001"
        // : import.meta.env.MODE === "stg"
        // ? "https://stg.com" :
        // import.meta.env.MODE === "uat" ?
        // "https://uat.com" :
        // "https://www.baidu.com",

    timeout: 1000 * 10,//超时时间
    headers: {
        'x': 1,
        'y': 2,
        // Authorization:getToken(),
    }
})

//请求拦截器
request.interceptors.request.use((config) => {
    const token = getToken();
    if(token){
        config.headers.Authorization = token  //携带
    }
    return config
}, (error) => {
    return error
}
)

//响应拦截器,then
request.interceptors.response.use((response) => {
    console.log('响应1');
    const result = response.data
    if(result.code !== 1000){
        ElMessage.error(result.message || "网络异常，请稍后重试！！！")
        return Promise.reject(result)
    } else {
        return result.data
    }
    return response
}, (error) => {
    console.log('响应2');
    return Promise.reject(error)
}
)

export default request